home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1175 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.6 KB

  1. Path: cs.mu.OZ.AU!bounce-back
  2. From: ldh@interlog.com (Laurent-David Hasson)
  3. Newsgroups: comp.std.c++,comp.lang.c++
  4. Subject: Class instanciation and virtual stuff..
  5. Date: 20 Apr 96 03:52:59 GMT
  6. Organization: Discobole consulting
  7. Approved: fjh@cs.mu.oz.au
  8. Message-ID: <4l6vtu$qap@steel.interlog.com>
  9. NNTP-Posting-Host: munta.cs.mu.oz.au
  10. X-Original-Date: Fri, 19 Apr 96 03:05:05 GMT
  11. X-Newsreader: News Xpress 2.0 Beta #0
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMXhf7eEDnX0m9pzZAQHTigF/bZKJ0ZnOtOUFvGhEF2JP2keqph5l7rkJ
  14.     Cvj2KHTxHDPy2S0byuI7uD0H2EwJGyyV
  15.     =MUAm
  16. Originator: fjh@munta.cs.mu.OZ.AU
  17.  
  18.   I have a question about how objects are constructed, and when "virtual" 
  19. services become available.
  20.  
  21.   I remember reading in the ARM that one should not call "virtual" functions 
  22. from a constructor. The reason being that an object should not be considered 
  23. fully initialized until the last constructor had ended.
  24.  
  25.   However, by mistake, in one of my program, i was calling a library function
  26. which is calling virtual functions. I never noticed the error because
  27. apparently, the compiler i was using at the time allowed this behavior to
  28. work properly. However, i recently got a new version of the compiler, and
  29. everything broke. It took me several days to realize what was happening.
  30.  
  31.   I am now left with doubts with respect to the availibility of "virtual"
  32. services, and i am now wondering why it is the way it is, that is, why a
  33. class type should not be known, and all the plumbing in place, before the
  34. first constructor is called. Naively, i would assume that instanciating an
  35. object would go as follows:
  36.       - Allocate the memory for the whole class
  37.       - Create the suporting structural stuff (like typeID, VT etc...)
  38.       - Then call the constructor, which follows with the Yo-Yo construction
  39.        to initialize all the data members and whatever.
  40.  
  41.   This makes sense to me because the type of the class would actualy be known
  42. in the constructor of the base class. Instead, calling "typeid" on "this" in
  43. the base constructor gets the base class' typename. I understand why one
  44. should not lightly call a virtual function because the local vars would not
  45. have been initialized yet. But what if i called a virtual function that did
  46. initialize vars it needed (never access a var of the class before it has been
  47. written to first)? Or a virtual function that does not use the local
  48. members?  Why should that fail?
  49.  
  50.  This leads me to think that all the "virtual" things are actually taken care
  51.  of AFTER all construction has been made. And i do not understand why this
  52. is. Are there important penalties i do not understand, or pitfalls i do not
  53. see?
  54.  
  55.  
  56.  
  57. Thanks,
  58. Laurent.
  59.  
  60.  
  61. PS: CC by email is much appreciated.
  62.  
  63. ============================================================================
  64. Microsoft Network, or any service owned in full or in part by Microsoft, is
  65. prohibited from redistributing this work in any form, in whole or in part.
  66. Copyright, Laurent Hasson, 1996. License to distribute this post is
  67. available to Microsoft for US$10,000 per instance. Posting without 
  68. permission constitutes an agreement to these terms. Please send notices of 
  69. violation to ldh@interlog.com and postmaster@microsoft.com
  70. ============================================================================
  71. ---
  72. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  73. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  74. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  75. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  76. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  77.